import { VantComponent } from '../common/component';
import wepy from 'wepy';
import addressApi from '../../../api/address';
import Toast from '../toast/toast';
import wxutils from '../../../utils/WxUtils';
import amapUtils from '../../../utils/amapUtils';
import Tips from '../../../utils/Tips';

VantComponent({
  props: {},
  data:{
    fileImage:"",
  },
  mounted: function () {
    ///渲染前的生命周期    
    if(wepy.$instance.globalData.CdnUrl){
      let fileImage= `${wepy.$instance.globalData.CdnUrl}/xkdnewyun/systemfile/wxashop/icon/wechat.png`;
      this.setData({fileImage: fileImage});
    }else{
      setTimeout(()=>{
        let fileImage= `${wepy.$instance.globalData.CdnUrl}/xkdnewyun/systemfile/wxashop/icon/wechat.png`;        
        this.setData({fileImage: fileImage});             
      },2000)
    }
    Toast.setDefaultOptions({selector: '#wvan-toast',context:this});
  },
  methods: {
    onClickWechat: function onClickWechat() {  
     let that=this;
     wx.chooseAddress({
        success(wxRs) {
          Tips.loading();
          amapUtils.getAmapLocation(wxRs.cityName,wxRs.provinceName+wxRs.cityName+wxRs.countyName+wxRs.detailInfo).then(amapRs=>{
            
            if(amapRs.status!="1" || !amapRs.geocodes || amapRs.geocodes.length<1 || !amapRs.geocodes[0].location){
              Tips.loaded();
              Tips.error("解析微信地址出错了，地图上无法找到当前地址信息");//转换出错了
              wxutils.logNotify("地址转经纬度", amapRs);
              return;
            }

            let mpInfo=amapRs.geocodes[0];
            let location=mpInfo.location.split(",");
            let fmtAddress=mpInfo.formatted_address;
            
            let province=mpInfo.province;
            let city=mpInfo.city;
            let district=mpInfo.district;
           
            if(Array.isArray(province)){
              province="";///可能不存在
            }

            if(Array.isArray(city)){
              city="";///可能不存在
            }
            
            if(Array.isArray(district)){
              district="";///可能不存在
            }
  
            let pcd=province+city+district;
            fmtAddress=fmtAddress.replace("|","");
            fmtAddress=fmtAddress.replace(pcd,"");////得到真实定位的地址
            
            pcd=city+district;
            fmtAddress=fmtAddress.replace(pcd,"");////得到真实定位的地址
  
            pcd=district;
            fmtAddress=fmtAddress.replace(pcd,"");////得到真实定位的地址
            
            let detail="";
            if(wxRs.detailInfo)
            {
              detail=wxRs.detailInfo.replace(fmtAddress,"");
            }
                   
          if(detail==""){
            detail=".";
          }
      　

            addressApi.AddWeixinAddress({
                    userName: wxRs.userName,
                    phone: wxRs.telNumber==='020-81167888'?'13112345678':wxRs.telNumber,
                    provName: wxRs.provinceName,
                    cityName: wxRs.cityName,
                    districtName: wxRs.countyName,
                    detail: detail,
                    streatName: fmtAddress,
                    lat:location[1],
                    lng:location[0],
                }).then(resData=>{
                  
                  if(resData.Code ===0) {
                    addressApi.GetMemberAddressById({
                        id:resData.Data
                    }).then(valData => {
                        Tips.loaded();
                        valData.Data.Detail=valData.Data.StreetName+valData.Data.Detail;
                        
                        if(valData.Code ===0) {
                            that.$emit('wechataddress', valData.Data);
                        }                   
                    })
                 }
                 else{
                  Tips.loaded();
                  Tips.error(resData.Msg);
                 }
              })

          }).catch(ex=>{
            Tips.loaded();
            if(ex&&ex.errMsg=="request:fail url not in domain list"){
              Tips.error("高德地图安全域名未配置");
            }
            else{
              Tips.error(ex.errMsg);
            }
          });

        }
    })
    },
    onClickAdd: function onClickAdd() {
        let link ="/pages/member/addAddress?cb=SetListData"; 
        wxutils.redirectToPage(link);
    },
  }
});